<!DOCTYPE HTML>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" /> 
    <title> - 天地维杰网</title>
    <meta name="keywords" content="系统架构,shutdown,不与天斗,Domino,博客,程序员,架构师,笔记,技术,分享,java,Redis">
    
    <meta property="og:title" content="">
    <meta property="og:site_name" content="天地维杰网">
    <meta property="og:image" content="/img/author.jpg"> 
    <meta name="title" content=" - 天地维杰网" />
    <meta name="description" content="天地维杰网 | 博客 | 软件 | 架构 | Java "> 
    <link rel="shortcut icon" href="http://www.shutdown.cn/img/favicon.ico" />
    <link rel="apple-touch-icon" href="http://www.shutdown.cn/img/apple-touch-icon.png" />
    <link rel="apple-touch-icon-precomposed" href="http://www.shutdown.cn/img/apple-touch-icon.png" />
    <link href="http://www.shutdown.cn/js/vendor/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/js/vendor/fancybox/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/css/main.css" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/css/syntax.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Pisces',
    sidebar: {"position":"left","display":"post"},
     fancybox: true, 
    motion: true
  };
</script>
</head>
<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">
<div class="container one-collumn sidebar-position-left page-home  ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"> <div class="site-meta  custom-logo ">

  <div class="custom-logo-site-title">
    <a href="http://www.shutdown.cn"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">天地维杰网</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle">人如秋鸿来有信，事若春梦了无痕</p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
    <ul id="menu" class="menu">
      
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />首页
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/redis/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-battery-full"></i> <br />Redis
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/java/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-coffee"></i> <br />java
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/linux/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-linux"></i> <br />linux
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/daily/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-bug"></i> <br />日常问题
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/spring/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-child"></i> <br />Spring和Springboot
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/spring/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-fire"></i> <br />Mac相关
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/middleware/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-gavel"></i> <br />中间件
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/jiagou/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-rocket"></i> <br />架构
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/python/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-ship"></i> <br />python
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/front/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-bolt"></i> <br />前端
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/jvm/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-balance-scale"></i> <br />JVM
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/post/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />归档
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/about/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />关于
          </a>
        </li>
      
      <li class="menu-item menu-item-search">
        <a href="javascript:;" class="popup-trigger"> <i class="menu-item-icon fa fa-search fa-fw"></i> <br /> 搜索</a>
      </li>
    </ul>
    <div class="site-search">
      <div class="popup">
 <span class="search-icon fa fa-search"></span>
 <input type="text" id="local-search-input">
 <div id="local-search-result"></div>
 <span class="popup-btn-close">close</span>
</div>

    </div>
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            
<section id="posts" class="posts-expand">
  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <header class="post-header">
      <h1 class="post-title" itemprop="name headline">
        <a class="post-title-link" href="http://www.shutdown.cn/post/jvm%E5%AD%A6%E4%B9%A004-%E5%9E%83%E5%9C%BE%E5%9B%9E%E6%94%B6%E6%A6%82%E5%BF%B5%E4%B8%8E%E7%AE%97%E6%B3%95/" itemprop="url">
        
        </a>
      </h1>
      <div class="post-meta">
      <span class="post-time">
<span class="post-meta-item-icon">
    <i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">时间：</span>
<time itemprop="dateCreated" datetime="2016-03-22T13:04:35+08:00" content="0001-01-01">
    0001-01-01
</time>
</span> 
      
      
       <span>
&nbsp; | &nbsp;
<span class="post-meta-item-icon">
    <i class="fa fa-eye"></i>
</span>
<span class="post-meta-item-text">阅读：</span>
<span class="leancloud-visitors-count">425 字 ~2分钟</span>
</span>
      </div>
    </header>
    <div class="post-body" itemprop="articleBody">
    

    

<h1 id="jvm学习04-垃圾回收概念与算法">JVM学习04-垃圾回收概念与算法</h1>

<h2 id="一-常用的垃圾回收算法">一、常用的垃圾回收算法</h2>

<ol>
<li>引用计数法</li>
</ol>

<p>为对象配备一个整型计数器，只要有任何一个对象引用了这个对象，这个对象的计数器就加1，引用失效时引用计数器就减1。只要对象的引用计数器的值为0，则对象就不可能再被使用。</p>

<p>问题：</p>

<ul>
<li><p>无法处理循环引用的情况。</p>

<p><div  align="center"><img src="https://wejack639.oss-cn-beijing.aliyuncs.com/blogimages/img/1623827473810-20210616151113.png" width = 100 /> </div></p></li>

<li><p>每次因引用产生和消除，都需要伴随一个加法和减法操作，对系统性能会有一定影响。</p></li>
</ul>

<p>Java虚拟机并未选择此算法作为垃圾回收算法。</p>

<ol>
<li>标记清除法（Mark-Sweep）</li>
</ol>

<p>标记清除算法将垃圾回收分为两个阶段：标记阶段和清除阶段。</p>

<ul>
<li>标记阶段，通过根节点标记所有的从根节点开始的可达对象。未被标记的对象就是未被引用的垃圾对象</li>
<li>清除阶段，清除所有未被标记的对象。</li>
</ul>

<p><div  align="center"><img src="https://wejack639.oss-cn-beijing.aliyuncs.com/blogimages/img/1623827963377-20210616151922.png" width = 400 /> </div></p>

<p>问题：回收后的空间不连续，会产生空间碎片。在对象的堆空间分配过程中，尤其是大对象的内在分配，不连续内在空间的工作效率要低于连续空间。</p>

<ol>
<li>复制算法</li>
</ol>

<p>将原有的内在空间分为两块，每次只使用其中一块，在垃圾回收时，将正在使用的内存中存活的对象复制到未使用的内在块中，之后，清除正在使用的内存块中的所有对象，交换两个内存的角色，完成垃圾回收。</p>

<p><div  align="center"><img src="https://wejack639.oss-cn-beijing.aliyuncs.com/blogimages/img/1624618290531-20210625185129.png" width = 600 /> </div></p>

<ul>
<li><p>优点：效率高、没碎片</p>

<p>效率高体现在：如果垃圾多，要复制的存活对象相对会少。</p>

<p>没碎片体现在：对象在垃圾回收过程中被统一复制到新内存空间中，所以可确保没碎片。</p></li>

<li><p>缺点：系统内存折半</p></li>
</ul>

<p>在Java新生代串行垃圾回收器中，使用了复制算法思想。新生代分为eden、from、to三个部分。from和to可视为用于复制的两块大小相同、地位相等且可进行角色互换的空间块。</p>

<p>from和to空间也被称为survivor空间，即幸存者空间用于存放未被回收的对象，如下图。</p>

<p><div  align="center"><img src="https://wejack639.oss-cn-beijing.aliyuncs.com/blogimages/img/1624618719746-20210625185838.png" width = 600 /> </div></p>

<ul>
<li><em>新生代</em> ：存放年轻对象的堆空间。年轻对象指刚刚创建的或经历垃圾回收次数不多的对象。</li>
<li>老年代：存放老年对象的堆空间。老年对象就是经历多次垃圾回收依然存活的对象。</li>
</ul>

<p>垃圾加收时，eden空间中存活的对象，会被复制到未使用的survivor空间中(假设是to )，正在使用的survivor空间(假设是from) 中的年轻对象也会被复制到to空间中(大对象或者老年对象会直接进入老年代，如果to空间满了，对象也会直接进入老年代)。此时 eden和from空间中的剩余对象就是垃圾对象，可以直接清空，to空间则存放此次回收后的存活对象。</p>

<p>这种改进的复制算法既保证了空间的连续性，又避免了大量的内存空间的浪费。</p>

<p>PS:复制算法比较适合用于新生代。因为在新生代，垃圾对象通常会多于存活对象，复制算法的效果会比较好。</p>

<ol>
<li>标记压缩法</li>
</ol>

<p>老年代大部分对象是存活对象，标记压缩法是一种老年代的回收算法，先从根节点开始对所有可达对象做一次标记，之后将所有的存活对象压缩到内存的一端，之后清理边界外所有的空间。</p>

<p>优点：既避免了碎片的产生，又不需要两块相同的内在空间，性价比较高。</p>

<p><div  align="center"><img src="https://wejack639.oss-cn-beijing.aliyuncs.com/blogimages/img/1625563415129-20210706172333.png" width = 600 /> </div></p>

<ol>
<li>分代算法</li>
</ol>

<p>即前面的汇总，不同的内存区间使用不同的回收算法。</p>

<p><div  align="center"><img src="https://wejack639.oss-cn-beijing.aliyuncs.com/blogimages/img/1625563815960-20210706173015.png" width = 600 /> </div></p>

<p>新生代回收频率高，每次回收耗时短</p>

<p>老年代回收频率低，耗时长。</p>

<p>为了支持高频率的新生代回收，虚拟机可能使用一种叫卡表的数据结构。卡表为一个比特位集合，每个比特位表示老年代区域中所有对象是否持有新生代的对象引用。这样在新生代GC时，可以不用花大量时间扫描所有老年代对象来确定每个对象的引用关系，先扫描卡表，只有卡表的标记位为1时，才需要扫描特定老年代对象。卡表位为0的所在区域必定不含有新生代对象引用。</p>

<p><div  align="center"><img src="https://wejack639.oss-cn-beijing.aliyuncs.com/blogimages/img/1625567480369-20210706183119.png" width = 600 /> </div></p>

<ol>
<li>分区算法</li>
</ol>

<p>分区算法将整个堆空间划分为连续不同的小区间，每个小区间都独立使用独立回收。</p>

<p>优点：可以控制一次回收多少个小区间，更好控制GC产生的停顿时间。每次合理地回收若干个小区间，而不是整个堆空间。<font color=red>（有没有使用场景？比如什么版本的的虚拟机这么用）</font></p>

<h2 id="二-判断可触及性">二、判断可触及性</h2>

<p>可触及性包含三种状态</p>

<ul>
<li>可触及：从根节点开始，可以到达这个对象</li>
<li>可复活:对象的所有引用都被释放，但对象有可能在finalize()函数中复活。</li>
<li>不可触及的：对象的finalize()函数被调用，并且没有复活，就会进入不可触及状态。</li>
</ul>

<p>以上三种状态中，只有对象不可触及时，才可以被回收。</p>

<ol>
<li><p>对象复活</p>

<ul>
<li>finalize方法至多由GC执行一次(用户当然可以手动调用对象的finalize方法，但并不影响GC对finalize的行为)</li>
<li>finalize()函数可能引用外泄，无意中复活对象</li>

<li><p>finalize()被系统调用，调用时间不明确，推荐在try-catch-finally中进行资源释放。</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java"><span style="color:#204a87;font-weight:bold">public</span> <span style="color:#204a87;font-weight:bold">class</span> <span style="color:#000">CanReliveObj</span> <span style="color:#ce5c00;font-weight:bold">{</span>
       
<span style="color:#204a87;font-weight:bold">public</span> <span style="color:#204a87;font-weight:bold">static</span> <span style="color:#000">CanReliveObj</span> <span style="color:#000">obj</span><span style="color:#ce5c00;font-weight:bold">;</span>
   
<span style="color:#204a87;font-weight:bold">public</span> <span style="color:#204a87;font-weight:bold">static</span> <span style="color:#204a87;font-weight:bold">void</span> <span style="color:#000">main</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">String</span><span style="color:#ce5c00;font-weight:bold">[</span><span style="color:#ce5c00;font-weight:bold">]</span> <span style="color:#000">args</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#204a87;font-weight:bold">throws</span> <span style="color:#000">InterruptedException</span> <span style="color:#ce5c00;font-weight:bold">{</span>
   <span style="color:#000">obj</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87;font-weight:bold">new</span> <span style="color:#000">CanReliveObj</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
   <span style="color:#000">obj</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87;font-weight:bold">null</span><span style="color:#ce5c00;font-weight:bold">;</span>
 	<span style="color:#8f5902;font-style:italic">//这里调用 gc以后，会调用到finalize()方法，将obj对象复活
</span><span style="color:#8f5902;font-style:italic"></span>   <span style="color:#000">System</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">gc</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
   <span style="color:#000">Thread</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">sleep</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">1000</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
   <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">obj</span> <span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87;font-weight:bold">null</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">{</span>
       <span style="color:#000">System</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">out</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">println</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#4e9a06">&#34;obj 是 null&#34;</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
   <span style="color:#ce5c00;font-weight:bold">}</span> <span style="color:#204a87;font-weight:bold">else</span> <span style="color:#ce5c00;font-weight:bold">{</span>
       <span style="color:#000">System</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">out</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">println</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#4e9a06">&#34;obj 可用&#34;</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
   <span style="color:#ce5c00;font-weight:bold">}</span>
   <span style="color:#000">System</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">out</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">println</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#4e9a06">&#34;第二次gc&#34;</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
   <span style="color:#000">obj</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87;font-weight:bold">null</span><span style="color:#ce5c00;font-weight:bold">;</span>
 	<span style="color:#8f5902;font-style:italic">// finalize()方法这次不会被调用，所以这回 obj对象真正被回收
</span><span style="color:#8f5902;font-style:italic"></span>   <span style="color:#000">System</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">gc</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
   <span style="color:#000">Thread</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">sleep</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">1000</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
   <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">obj</span> <span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87;font-weight:bold">null</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">{</span>
       <span style="color:#000">System</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">out</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">println</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#4e9a06">&#34;obj 是 null&#34;</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
   <span style="color:#ce5c00;font-weight:bold">}</span> <span style="color:#204a87;font-weight:bold">else</span> <span style="color:#ce5c00;font-weight:bold">{</span>
       <span style="color:#000">System</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">out</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">println</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#4e9a06">&#34;obj 可用&#34;</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
   <span style="color:#ce5c00;font-weight:bold">}</span>
<span style="color:#ce5c00;font-weight:bold">}</span>
   
<span style="color:#5c35cc;font-weight:bold">@Override</span>
<span style="color:#204a87;font-weight:bold">protected</span> <span style="color:#204a87;font-weight:bold">void</span> <span style="color:#000">finalize</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#204a87;font-weight:bold">throws</span> <span style="color:#000">Throwable</span> <span style="color:#ce5c00;font-weight:bold">{</span>
   <span style="color:#204a87;font-weight:bold">super</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">finalize</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
   <span style="color:#000">System</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">out</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">println</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#4e9a06">&#34;CanReliveObj finalize called&#34;</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
   <span style="color:#000">obj</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87;font-weight:bold">this</span><span style="color:#ce5c00;font-weight:bold">;</span>
<span style="color:#ce5c00;font-weight:bold">}</span>
   
<span style="color:#5c35cc;font-weight:bold">@Override</span>
<span style="color:#204a87;font-weight:bold">public</span> <span style="color:#000">String</span> <span style="color:#000">toString</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">{</span>
   <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#4e9a06">&#34;I am CanReliveObj&#34;</span><span style="color:#ce5c00;font-weight:bold">;</span>
<span style="color:#ce5c00;font-weight:bold">}</span>
<span style="color:#ce5c00;font-weight:bold">}</span></code></pre></div></li>
</ul></li>

<li><p>引用和可触及性的强度</p></li>
</ol>

<p>强引用、软引用、弱引用、虚引用，不知道干啥用的，先略过。</p>

<p>强引用特点：</p>

<ul>
<li>强引用可以直接访问目标对象</li>
<li>强引用指向的对象在任何时候都不会被系统回收，虚拟机宁可OOM，也不回收强引用指向的对象</li>

<li><p>强引用可能导致内存泄漏</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java"><span style="color:#204a87;font-weight:bold">public</span> <span style="color:#204a87;font-weight:bold">class</span> <span style="color:#000">StrongReference</span> <span style="color:#ce5c00;font-weight:bold">{</span>
   <span style="color:#204a87;font-weight:bold">public</span> <span style="color:#204a87;font-weight:bold">static</span> <span style="color:#204a87;font-weight:bold">void</span> <span style="color:#000">main</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">String</span><span style="color:#ce5c00;font-weight:bold">[</span><span style="color:#ce5c00;font-weight:bold">]</span> <span style="color:#000">args</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">{</span>
       <span style="color:#000">StringBuilder</span> <span style="color:#000">sb1</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87;font-weight:bold">new</span> <span style="color:#000">StringBuilder</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#4e9a06">&#34;abcde&#34;</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
       <span style="color:#000">StringBuilder</span> <span style="color:#000">sb2</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">sb1</span><span style="color:#ce5c00;font-weight:bold">;</span>
       <span style="color:#000">System</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">out</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">println</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">sb1</span> <span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">sb2</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
       <span style="color:#000">System</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">out</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">println</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">sb1</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">equals</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">sb2</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
   
       <span style="color:#000">sb1</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87;font-weight:bold">new</span> <span style="color:#000">StringBuilder</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#4e9a06">&#34;fgihjk&#34;</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
       <span style="color:#000">System</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">out</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">println</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">sb1</span> <span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">sb2</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
       <span style="color:#000">System</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">out</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">println</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">sb1</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">equals</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">sb2</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
   <span style="color:#ce5c00;font-weight:bold">}</span>
<span style="color:#ce5c00;font-weight:bold">}</span>
<span style="color:#000">运行结果</span>
<span style="color:#204a87;font-weight:bold">true</span>
<span style="color:#204a87;font-weight:bold">true</span>
<span style="color:#204a87;font-weight:bold">false</span>
<span style="color:#204a87;font-weight:bold">false</span></code></pre></div></li>
</ul>

<p><div  align="center"><img src="https://wejack639.oss-cn-beijing.aliyuncs.com/blogimages/img/1625571008216-20210706193007.png" width = 400 /> </div></p>

<p><div  align="center"><img src="https://wejack639.oss-cn-beijing.aliyuncs.com/blogimages/img/1625571085350-20210706193124.png" width = 400 /> </div></p>

<ol>
<li>垃圾回收时的停顿现象：Stop-The-World</li>
</ol>

<p><div  align="center"><img src="https://wejack639.oss-cn-beijing.aliyuncs.com/blogimages/img/1625571347878-20210706193547.png" width = 600 /> </div></p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java">   
   
   <span style="color:#204a87;font-weight:bold">import</span> <span style="color:#000">java.util.HashMap</span><span style="color:#ce5c00;font-weight:bold">;</span>
   
   <span style="color:#8f5902;font-style:italic">/**
</span><span style="color:#8f5902;font-style:italic">    * -Xmx1g -Xms1g -Xmn512k -XX:+UseSerialGC -XX:+PrintGCDetails -Xloggc:StopWorldTestGcLog.log
</span><span style="color:#8f5902;font-style:italic">    * @author dmn
</span><span style="color:#8f5902;font-style:italic">    */</span>
   <span style="color:#204a87;font-weight:bold">public</span> <span style="color:#204a87;font-weight:bold">class</span> <span style="color:#000">StopWorldTest</span> <span style="color:#ce5c00;font-weight:bold">{</span>
   
       <span style="color:#204a87;font-weight:bold">public</span> <span style="color:#204a87;font-weight:bold">static</span> <span style="color:#204a87;font-weight:bold">void</span> <span style="color:#000">main</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">String</span> <span style="color:#000">args</span><span style="color:#ce5c00;font-weight:bold">[</span><span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">{</span>
           <span style="color:#000">MyThread</span> <span style="color:#000">t</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87;font-weight:bold">new</span> <span style="color:#000">MyThread</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
           <span style="color:#000">PrintThread</span> <span style="color:#000">p</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87;font-weight:bold">new</span> <span style="color:#000">PrintThread</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
           <span style="color:#000">t</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">start</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
           <span style="color:#000">p</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">start</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
       <span style="color:#ce5c00;font-weight:bold">}</span>
   
     	<span style="color:#8f5902;font-style:italic">/**
</span><span style="color:#8f5902;font-style:italic">     	* 不断申请内存操作
</span><span style="color:#8f5902;font-style:italic">     	*/</span>
       <span style="color:#204a87;font-weight:bold">public</span> <span style="color:#204a87;font-weight:bold">static</span> <span style="color:#204a87;font-weight:bold">class</span> <span style="color:#000">MyThread</span> <span style="color:#204a87;font-weight:bold">extends</span> <span style="color:#000">Thread</span> <span style="color:#ce5c00;font-weight:bold">{</span>
           <span style="color:#000">HashMap</span> <span style="color:#000">map</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87;font-weight:bold">new</span> <span style="color:#000">HashMap</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
   
           <span style="color:#5c35cc;font-weight:bold">@Override</span>
           <span style="color:#204a87;font-weight:bold">public</span> <span style="color:#204a87;font-weight:bold">void</span> <span style="color:#000">run</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">{</span>
               <span style="color:#204a87;font-weight:bold">try</span> <span style="color:#ce5c00;font-weight:bold">{</span>
                   <span style="color:#204a87;font-weight:bold">while</span> <span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">true</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">{</span>
   <span style="color:#8f5902;font-style:italic">//                    System.out.println((map.size() * 512) / 1024 / 1024);
</span><span style="color:#8f5902;font-style:italic"></span>                       <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">map</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">size</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">*</span> <span style="color:#000">512</span> <span style="color:#ce5c00;font-weight:bold">/</span> <span style="color:#000">1024</span> <span style="color:#ce5c00;font-weight:bold">/</span> <span style="color:#000">1024</span> <span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">880</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">{</span>
                           <span style="color:#000">map</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">clear</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
                           <span style="color:#000">System</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">out</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">println</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#4e9a06">&#34;clean map&#34;</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
                       <span style="color:#ce5c00;font-weight:bold">}</span>
                       <span style="color:#204a87;font-weight:bold">byte</span><span style="color:#ce5c00;font-weight:bold">[</span><span style="color:#ce5c00;font-weight:bold">]</span> <span style="color:#000">b1</span><span style="color:#ce5c00;font-weight:bold">;</span>
                       <span style="color:#204a87;font-weight:bold">for</span> <span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">i</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">0</span><span style="color:#ce5c00;font-weight:bold">;</span> <span style="color:#000">i</span> <span style="color:#ce5c00;font-weight:bold">&lt;</span> <span style="color:#000">100</span><span style="color:#ce5c00;font-weight:bold">;</span> <span style="color:#000">i</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#ce5c00;font-weight:bold">+</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">{</span>
                           <span style="color:#000">b1</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87;font-weight:bold">new</span> <span style="color:#204a87;font-weight:bold">byte</span><span style="color:#ce5c00;font-weight:bold">[</span><span style="color:#000">512</span><span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#ce5c00;font-weight:bold">;</span>
                           <span style="color:#000">map</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">put</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">System</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">nanoTime</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#000">b1</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
                       <span style="color:#ce5c00;font-weight:bold">}</span>
                       <span style="color:#000">Thread</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">sleep</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">1</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
                   <span style="color:#ce5c00;font-weight:bold">}</span>
               <span style="color:#ce5c00;font-weight:bold">}</span> <span style="color:#204a87;font-weight:bold">catch</span> <span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">Exception</span> <span style="color:#000">e</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">{</span>
   
               <span style="color:#ce5c00;font-weight:bold">}</span>
           <span style="color:#ce5c00;font-weight:bold">}</span>
       <span style="color:#ce5c00;font-weight:bold">}</span>
   
     	<span style="color:#8f5902;font-style:italic">/**
</span><span style="color:#8f5902;font-style:italic">     	* 输出时间
</span><span style="color:#8f5902;font-style:italic">     	*/</span>
       <span style="color:#204a87;font-weight:bold">public</span> <span style="color:#204a87;font-weight:bold">static</span> <span style="color:#204a87;font-weight:bold">class</span> <span style="color:#000">PrintThread</span> <span style="color:#204a87;font-weight:bold">extends</span> <span style="color:#000">Thread</span> <span style="color:#ce5c00;font-weight:bold">{</span>
           <span style="color:#204a87;font-weight:bold">public</span> <span style="color:#204a87;font-weight:bold">static</span> <span style="color:#204a87;font-weight:bold">final</span> <span style="color:#204a87;font-weight:bold">long</span> <span style="color:#000">starttime</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">System</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">currentTimeMillis</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
   
           <span style="color:#5c35cc;font-weight:bold">@Override</span>
           <span style="color:#204a87;font-weight:bold">public</span> <span style="color:#204a87;font-weight:bold">void</span> <span style="color:#000">run</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">{</span>
               <span style="color:#204a87;font-weight:bold">try</span> <span style="color:#ce5c00;font-weight:bold">{</span>
                   <span style="color:#204a87;font-weight:bold">while</span> <span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">true</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">{</span>
                       <span style="color:#204a87;font-weight:bold">long</span> <span style="color:#000">t</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">System</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">currentTimeMillis</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">-</span> <span style="color:#000">starttime</span><span style="color:#ce5c00;font-weight:bold">;</span>
                       <span style="color:#000">System</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">out</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">println</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">t</span> <span style="color:#ce5c00;font-weight:bold">/</span> <span style="color:#000">1000</span> <span style="color:#ce5c00;font-weight:bold">+</span> <span style="color:#4e9a06">&#34;.&#34;</span> <span style="color:#ce5c00;font-weight:bold">+</span> <span style="color:#000">t</span> <span style="color:#ce5c00;font-weight:bold">%</span> <span style="color:#000">1000</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
                       <span style="color:#000">Thread</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">sleep</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">100</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">;</span>
                   <span style="color:#ce5c00;font-weight:bold">}</span>
               <span style="color:#ce5c00;font-weight:bold">}</span> <span style="color:#204a87;font-weight:bold">catch</span> <span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">Exception</span> <span style="color:#000">e</span><span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">{</span>
   
               <span style="color:#ce5c00;font-weight:bold">}</span>
           <span style="color:#ce5c00;font-weight:bold">}</span>
       <span style="color:#ce5c00;font-weight:bold">}</span>
   <span style="color:#ce5c00;font-weight:bold">}</span></code></pre></div>
<p>日志里可以看到 [Times: user=0.83 sys=0.01, real=0.85 secs] 的real就是Full GC实际花费的时间</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell">   29.998: <span style="color:#ce5c00;font-weight:bold">[</span>Full GC <span style="color:#ce5c00;font-weight:bold">(</span>Allocation Failure<span style="color:#ce5c00;font-weight:bold">)</span> 29.998: <span style="color:#ce5c00;font-weight:bold">[</span>Tenured: 1048063K-&gt;1048063K<span style="color:#ce5c00;font-weight:bold">(</span>1048064K<span style="color:#ce5c00;font-weight:bold">)</span>, 0.7081273 secs<span style="color:#ce5c00;font-weight:bold">]</span> 1048511K-&gt;1048461K<span style="color:#ce5c00;font-weight:bold">(</span>1048512K<span style="color:#ce5c00;font-weight:bold">)</span>, <span style="color:#ce5c00;font-weight:bold">[</span>Metaspace: 8695K-&gt;8695K<span style="color:#ce5c00;font-weight:bold">(</span>1056768K<span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">]</span>, 0.7083946 secs<span style="color:#ce5c00;font-weight:bold">]</span> <span style="color:#ce5c00;font-weight:bold">[</span>Times: <span style="color:#000">user</span><span style="color:#ce5c00;font-weight:bold">=</span>0.70 <span style="color:#000">sys</span><span style="color:#ce5c00;font-weight:bold">=</span>0.01, <span style="color:#000">real</span><span style="color:#ce5c00;font-weight:bold">=</span>0.71 secs<span style="color:#ce5c00;font-weight:bold">]</span> 
   30.707: <span style="color:#ce5c00;font-weight:bold">[</span>Full GC <span style="color:#ce5c00;font-weight:bold">(</span>Allocation Failure<span style="color:#ce5c00;font-weight:bold">)</span> 30.707: <span style="color:#ce5c00;font-weight:bold">[</span>Tenured: 1048063K-&gt;1048063K<span style="color:#ce5c00;font-weight:bold">(</span>1048064K<span style="color:#ce5c00;font-weight:bold">)</span>, 0.7488220 secs<span style="color:#ce5c00;font-weight:bold">]</span> 1048511K-&gt;1048511K<span style="color:#ce5c00;font-weight:bold">(</span>1048512K<span style="color:#ce5c00;font-weight:bold">)</span>, <span style="color:#ce5c00;font-weight:bold">[</span>Metaspace: 8593K-&gt;8593K<span style="color:#ce5c00;font-weight:bold">(</span>1056768K<span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">]</span>, 0.7489386 secs<span style="color:#ce5c00;font-weight:bold">]</span> <span style="color:#ce5c00;font-weight:bold">[</span>Times: <span style="color:#000">user</span><span style="color:#ce5c00;font-weight:bold">=</span>0.73 <span style="color:#000">sys</span><span style="color:#ce5c00;font-weight:bold">=</span>0.01, <span style="color:#000">real</span><span style="color:#ce5c00;font-weight:bold">=</span>0.75 secs<span style="color:#ce5c00;font-weight:bold">]</span> 
   31.456: <span style="color:#ce5c00;font-weight:bold">[</span>Full GC <span style="color:#ce5c00;font-weight:bold">(</span>Allocation Failure<span style="color:#ce5c00;font-weight:bold">)</span> 31.456: <span style="color:#ce5c00;font-weight:bold">[</span>Tenured: 1048063K-&gt;1031386K<span style="color:#ce5c00;font-weight:bold">(</span>1048064K<span style="color:#ce5c00;font-weight:bold">)</span>, 0.8560296 secs<span style="color:#ce5c00;font-weight:bold">]</span> 1048511K-&gt;1031386K<span style="color:#ce5c00;font-weight:bold">(</span>1048512K<span style="color:#ce5c00;font-weight:bold">)</span>, <span style="color:#ce5c00;font-weight:bold">[</span>Metaspace: 8563K-&gt;8563K<span style="color:#ce5c00;font-weight:bold">(</span>1056768K<span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">]</span>, 0.8560950 secs<span style="color:#ce5c00;font-weight:bold">]</span> <span style="color:#ce5c00;font-weight:bold">[</span>Times: <span style="color:#000">user</span><span style="color:#ce5c00;font-weight:bold">=</span>0.83 <span style="color:#000">sys</span><span style="color:#ce5c00;font-weight:bold">=</span>0.01, <span style="color:#000">real</span><span style="color:#ce5c00;font-weight:bold">=</span>0.85 secs<span style="color:#ce5c00;font-weight:bold">]</span> 
   
   </code></pre></div>
<p>用VisualGC看效果可太炫酷了。</p>

<p><div  align="center"><img src="https://wejack639.oss-cn-beijing.aliyuncs.com/blogimages/img/1625627872077-20210707111746.png" width = 600 /> </div></p>

    </div>
    <footer class="post-footer">
     

     <div class="post-nav">
    <div class="post-nav-next post-nav-item">
    
        <a href="http://www.shutdown.cn/post/jvm%E5%AD%A6%E4%B9%A003-%E5%B8%B8%E7%94%A8java%E8%99%9A%E6%8B%9F%E6%9C%BA%E5%8F%82%E6%95%B0/" rel="next" title="">
        <i class="fa fa-chevron-left"></i> 
        </a>
    
    </div>

    <div class="post-nav-prev post-nav-item">
    
        <a href="http://www.shutdown.cn/post/jvm%E5%AD%A6%E4%B9%A005-%E5%9E%83%E5%9C%BE%E5%9B%9E%E6%94%B6%E5%99%A8%E5%92%8C%E5%86%85%E5%AD%98%E5%88%86%E9%85%8D/" rel="prev" title="">
         <i class="fa fa-chevron-right"></i>
        </a>
    
    </div>
</div>
      
     
     
     






    </footer>
  </article>
</section>

          </div>
        </div>
        <div class="sidebar-toggle">
  <div class="sidebar-toggle-line-wrap">
    <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
    <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
    <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
  </div>
</div>
<aside id="sidebar" class="sidebar">
  <div class="sidebar-inner">

    <section class="site-overview sidebar-panel  sidebar-panel-active ">
      <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image"
        src="http://www.shutdown.cn/img/author.jpg"
        alt="不与天斗Domino" />
    <p class="site-author-name" itemprop="name">不与天斗Domino</p>
    <p class="site-description motion-element" itemprop="description"> 
        Programmer &amp; Architect</p>
</div>
      <nav class="site-state motion-element">
    <div class="site-state-item site-state-posts">
      <a href="http://www.shutdown.cn/post/">
        <span class="site-state-item-count">172</span>
        <span class="site-state-item-name">日志</span>
      </a>
    </div>
    <div class="site-state-item site-state-categories">    
        <a href="http://www.shutdown.cn/categories/">      
         
        <span class="site-state-item-count">10</span>
        
        <span class="site-state-item-name">分类</span>
        
        </a>
    </div>

    <div class="site-state-item site-state-tags">
        <a href="http://www.shutdown.cn/tags/">
         
        <span class="site-state-item-count">96</span>
        
        <span class="site-state-item-name">标签</span>
        </a>
    </div>
</nav>
      
      

      

      <div class="links-of-blogroll motion-element inline">
<script type="text/javascript" src="//rf.revolvermaps.com/0/0/8.js?i=&amp;m=0&amp;s=220&amp;c=ff0000&amp;cr1=ffffff&amp;f=arial&amp;l=33&amp;bv=35" async="async"></script>
</div>

    </section>
    
  </div>
</aside>

      </div>
    </main>
   
    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  <span itemprop="copyrightYear">  &copy; 
  2013 - 2022</span>
  <span class="with-love"><i class="fa fa-heart"></i></span>
  <span class="author" itemprop="copyrightHolder">天地维杰网</span>
  <span class="icp" itemprop="copyrightHolder"><a href="https://beian.miit.gov.cn/" target="_blank">京ICP备13019191号-1</a></span>
</div>
<div class="powered-by">
  Powered by - <a class="theme-link" href="http://gohugo.io" target="_blank" title="hugo" >Hugo v0.63.2</a>
</div>
<div class="theme-info">
  Theme by - <a class="theme-link" href="https://github.com/xtfly/hugo-theme-next" target="_blank"> NexT
  </a>
</div>


      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
      <span id="scrollpercent"><span>0</span>%</span>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/jquery/index.js?v=2.1.3"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/fastclick/lib/fastclick.min.js?v=1.0.6"></script> 
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/velocity/velocity.min.js?v=1.2.1"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/velocity/velocity.ui.min.js?v=1.2.1"></script>
<script src="http://www.shutdown.cn/js/vendor/ua-parser-js/dist/ua-parser.min.js?v=0.7.9"></script>

<script src="http://www.shutdown.cn/js/vendor/fancybox/jquery.fancybox.pack.js?v=2.1.5"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/utils.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/motion.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/affix.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/schemes/pisces.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/scrollspy.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/post-details.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/toc.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/bootstrap.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js"],
    jax: ["input/TeX", "output/HTML-CSS"],
    tex2jax: {
      inlineMath: [ ['$','$'] ],
      displayMath: [ ['$$','$$'] ],
      processEscapes: true
    },
    "HTML-CSS": { fonts: ["TeX"] }
  });
</script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML' async></script>
</body>
</html>